﻿using BCCommon;
using BCCommon.Enums;
using BCCommon.TMS.PushMessage;
using BCData.Common.PushMessageConfig;
using BCEntity.Common.EntityNameConst;
using BCEntity.Common.PushMessageConfig;
using BCEntity.MongoDB.PushMessage;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCService.Common.PushMessage.PushMessageSend.Driver
{
    public class PushMessageForDriverService : IPushMessageForDriverService
    {
        private readonly IRedisService redisService;
        private readonly IPushMessageConfigData pushMessageConfigData;

        public PushMessageForDriverService(IRedisService redisService,
            IPushMessageConfigData pushMessageConfigData)
        {
            this.redisService = redisService;
            this.pushMessageConfigData = pushMessageConfigData;
        }

        public PushMessageMongoEntity GetPushMessageMongoEntity(long[] userIds, PushMessageType pushMessageType,
            DateTime start, DateTime end)
        {
            PushMessageMongoEntity pushMessageMongoEntity = new PushMessageMongoEntity();

            PushMessageConfigEntity pushMessageConfigEntity = GetPushMessageConfigInfo(pushMessageType);
            switch (pushMessageType)
            {
                case PushMessageType.DriverTrainingScheduleNotification:
                    MakeDriverTrainingScheduleNotification(userIds, pushMessageConfigEntity, pushMessageMongoEntity, start, end);
                    break;
                default:
                    return null;
            }

            return pushMessageMongoEntity;
        }

        public PushMessageMongoEntity GetPushMessageMongoEntity(long userId, PushMessageType pushMessageType, string vehicleNo = null)
        {
            PushMessageMongoEntity pushMessageMongoEntity = new PushMessageMongoEntity();

            PushMessageConfigEntity pushMessageConfigEntity = GetPushMessageConfigInfo(pushMessageType);
            switch (pushMessageType)
            {
                case PushMessageType.DriverCancelPassed:
                    MakeVehicleCancelPassed(userId, vehicleNo, pushMessageConfigEntity, pushMessageMongoEntity);
                    break;
                default:
                    return null;
            }

            return pushMessageMongoEntity;
        }
        #region 司机取消订单审核未通过/通过
        private void MakeVehicleCancelPassed(long userId, string orderNo, PushMessageConfigEntity pushMessageConfigEntity, PushMessageMongoEntity pushMessageMongoEntity)
        {
            //亲，订单编号:{0} 取消订单审核未通过。  （结果：亲，订单编号:{0} 取消订单审核未通过。）
            string body = pushMessageConfigEntity.MessageTemplate;
            body = string.Format(body, orderNo);
            pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
            pushMessageMongoEntity.AppType = AppType.DriverApp;
            pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
            pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
            pushMessageMongoEntity.Body = body;
            pushMessageMongoEntity.CreateTime = DateTime.Now;
            pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
            {
                MessageType = PushMessageObjType.Validation.ToString(),
                OrderType = pushMessageConfigEntity.PushMessageType.ToString()
            }, GeneralContainer.JsonSerializerSettings);
            pushMessageMongoEntity.UserIds = new long[] { userId };
        }
        #endregion

        private void MakeDriverTrainingScheduleNotification(long[] userIds, PushMessageConfigEntity pushMessageConfigEntity,
            PushMessageMongoEntity pushMessageMongoEntity, DateTime start, DateTime end)
        {
            //亲，请您于{0}至{1} 时间段，到{2}，参加公司组织的集体培训。
            string body = pushMessageConfigEntity.MessageTemplate;
            body = string.Format(body, start.ToString("yyyy-MM-dd"), end.ToString("yyyy-MM-dd"));
            pushMessageMongoEntity.TemplateMessageId = Guid.NewGuid().ToString().Replace("-", "");
            pushMessageMongoEntity.AppType = AppType.PartnerApp;
            pushMessageMongoEntity.BarTypeId = GeneralContainer.ANDROID_NOTIFICATION_BAR_TYPE;
            pushMessageMongoEntity.Title = pushMessageConfigEntity.MessageTitle;
            pushMessageMongoEntity.Body = body;
            pushMessageMongoEntity.CreateTime = DateTime.Now;
            pushMessageMongoEntity.Obj = JsonConvert.SerializeObject(new
            {
                MessageType = PushMessageType.DriverTrainingScheduleNotification.ToString()
            }, GeneralContainer.JsonSerializerSettings);
            pushMessageMongoEntity.UserIds = userIds.ToArray();
        }

        #region 获取推送配置信息
        private PushMessageConfigEntity GetPushMessageConfigInfo(PushMessageType pushMessageType)
        {
            string key = string.Format("{0}:PushMessageType={1}", EntityNameConst.PushMessageConfigEntity, pushMessageType.ToString());
            var pushMessageConfigEntity = this.redisService.TryGet(key, () =>
            {
                return this.pushMessageConfigData.GetEntity(pushMessageType).GetAwaiter().GetResult();
            }, TimeSpan.FromDays(1));
            return pushMessageConfigEntity;
        }
        #endregion
    }
}
